﻿using BuildingBlocks.Messaging.Events;
using Discount.Grpc.Data;
using Discount.Grpc.Services;
using MassTransit;

namespace Discount.Grpc.Consumers
{
    public class OrderCreateEventConsumer(ILogger<OrderCreateEventConsumer> logger, DiscountContext dbContext) : IConsumer<OrderCreateEvent>
    {
        public async Task Consume(ConsumeContext<OrderCreateEvent> context)
        {
            // TODO: Create new order and start order fullfillment process
            logger.LogInformation("Integration Event handled: {IntegrationEvent}", context.Message.GetType().Name);

            var request = new LockDiscountRequest {
                CouponId = (context.Message.CouponId == 0 ? 1 : context.Message.CouponId),
                OrderId = context.Message.OrderId.ToString(),
                Qty = (int)context.Message.Qty,
            };

            await DiscountEx.Handle(request, dbContext);


        }

        
    }
}
